<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>商品编辑</span>
      </div>
      <el-form label-width="100px" class="form">
        <el-form-item label="商品名称">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-input v-model="formLabelAlign.category"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number
            v-model="formLabelAlign.price"
            :min="0"
            :max="100"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片地址">
          <el-upload
            class="avatar-uploader"
            :action="Api + '/goods/goods_img_upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="Api+formLabelAlign.imgUrl"
              :src="Api+formLabelAlign.imgUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input
            type="textarea"
            rows:4
            v-model="formLabelAlign.goodsDesc"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品ID">
          <el-input v-model="formLabelAlign.id"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="uploader"
            >编辑</el-button
          ></el-form-item
        >
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { goodsEdit } from '../../api/goods';
export default {
  data () {
    return {
      Api: 'http://127.0.0.1:5000',
      formLabelAlign: {
        category: "",
        price: '',
        imgUrl: "",
        goodsDesc: '',
        id: '',
        name: '',
      }
    }
  },
  methods: {
    //商品图片验证
    beforeAvatarUpload (file) {
      const isJPG = /[jpeg|png]/.test(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess (res, file) {
      let { code, msg, imgUrl } = res
      if (code == 0) {
        this.formLabelAlign.imgUrl = imgUrl
      }
    },
    //编辑后上传
    async uploader () {
      let { category, price, imgUrl, goodsDesc, id, name } = this.formLabelAlign
      imgUrl = imgUrl.substr(imgUrl.lastIndexOf('/') + 1)
      let res = await goodsEdit({ category, price, imgUrl, goodsDesc, id, name })
      let { code, msg } = res.data
      if (code == 0) {
        this.$router.push('/goods/goods-list')
      }
    }
  },
  created () {
    let { id } = this.$route.query
    this.formLabelAlign = { ...id }
  },
}
</script>

<style lang="less" scoped>
.form {
  width: 500px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px solid;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>